package com.example.demo_videos.service.impl;

import com.example.demo_videos.pojo.Comment;
import com.example.demo_videos.service.CommentService;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    public int insertDoc(Comment comment){
        int i = 0;
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "localhost",27017 );

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            MongoCollection<Document> collection = mongoDatabase.getCollection("comments");
            System.out.println("集合 test 选择成功");
            //插入文档
            /**
             * 1. 创建文档 org.bson.Document 参数为key-value的格式
             * 2. 创建文档集合List<Document>
             * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
             * */
            Document document = new Document("user",comment.getUser()).
                    append("content", comment.getContent()).
                    append("createDate", comment.getDate());
            List<Document> documents = new ArrayList<Document>();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("文档插入成功");
            i = 1;
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            i=0;
        }
        return i;
    }

    public List<Comment> selectDoc(){
        List<Comment> comments = new ArrayList<>();
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("comments");
            System.out.println("集合 test 选择成功");

            //检索所有文档
            /**
             * 1. 获取迭代器FindIterable<Document>
             * 2. 获取游标MongoCursor<Document>
             * 3. 通过游标遍历检索出的文档集合
             * */
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            while(mongoCursor.hasNext()){
                Document document = mongoCursor.next();
                Comment comment = new Comment();
                comment.setUser(document.getString("user"));
                comment.setContent(document.getString("content"));
                String fDate = sdf.format(document.getDate("createDate"));
                comment.setTime(fDate);
                comments.add(comment);
            }
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
        return comments;
    }

}


